package tcln.gxtx.client.widget.form;

import java.util.Date;

import tcln.gxtx.client.utils.date.DefaultGwtDateInstant;
import tcln.gxtx.client.utils.date.GwtDate;

import com.extjs.gxt.ui.client.widget.form.DateField;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * An extended version of GXT {@link DateField} which provides some extensions.
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class ExtendedDateField extends DateField {

    /**
     * Inherited constructor.
     */
    public ExtendedDateField() {
        super();
    }

    /**
     * Constructs this date field with a format pattern. The 'emptyText' of this field is automatically set to the given
     * format pattern.
     * 
     * @param formatPattern
     *            Format pattern for both showing data and parsing input data (entered in the editor field of this date
     *            field).
     * @param editable
     *            <code>true</code> to enable editing in the editor field of this date field.
     */
    public ExtendedDateField(String formatPattern, boolean editable) {
        setEditable(editable);
        getPropertyEditor().setFormat(DateTimeFormat.getFormat(formatPattern));
        setEmptyText(formatPattern.toLowerCase());
    }

    /**
     * Returns a {@link GwtDate} representing the date value under this field.
     * 
     * @return
     */
    public GwtDate getGwtDate() {
        Date date = getValue();
        if (date != null) {
            return DefaultGwtDateInstant.createDate(date);
        } else {
            return null;
        }
    }
}
